package controller

import (
	"github.com/gin-gonic/gin"
	"myserver/model"
	"myserver/param"
	"myserver/response"
	"myserver/service"
	"myserver/tool"
)

type ReservationController struct {
}

var ReservationService = service.ReservationService{}

func (c *ReservationController) Router(apiGroup *gin.RouterGroup) {
	apiGroup.GET("/reservation", c.gets)
	apiGroup.GET("/reservations", c.getPage)
	apiGroup.GET("/reservationUsers", c.getUserPage)
	apiGroup.POST("/reservation", c.posts)
	apiGroup.DELETE("/reservation", c.deletes)
	apiGroup.PUT("/reservation", c.puts)
	apiGroup.PUT("/refuseReservation", c.puts01)
	apiGroup.POST("/rsWork", c.rsWork)
	apiGroup.POST("/rsSubsidy", c.rsSubsidy)
}

func (c *ReservationController) gets(context *gin.Context) {
	query, err := context.GetQuery("ID")
	if err == false {
		response.Failed(context, response.Ok().AddData("msg", "参数解析失败"))
	}
	reservation := ReservationService.QueryReservation(query)
	if reservation.ID != 0 {
		response.Success(context, response.Ok().AddData("res", reservation))
	} else {
		response.Failed(context, response.Error().AddData("res", "查询失败 可能是未发现记录"))
	}
}

func (c *ReservationController) posts(context *gin.Context) {
	var reservation model.Reservation
	err := tool.Decode(context.Request.Body, &reservation)
	if reservation.ID != 0 && err != nil {
		response.Failed(context, response.Ok().AddData("msg", "参数解析失败"))
		return
	}
	res := ReservationService.CreateReservation(reservation)
	if res != 0 {
		response.Success(context, response.Ok().AddData("res", "添加成功"))
	} else {
		response.Failed(context, response.Error().AddData("res", "添加失败"))
	}
}

func (c *ReservationController) deletes(context *gin.Context) {
	var reservation model.Reservation
	err := context.ShouldBind(&reservation)
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := ReservationService.DeleteReservation(reservation)
	if res != 0 {
		response.Success(context, response.Ok().AddData("res", "添加成功"))
	} else {
		response.Failed(context, response.Error().AddData("res", "添加失败"))
	}
}

func (c *ReservationController) puts(context *gin.Context) {
	var reservation model.Reservation
	err := tool.Decode(context.Request.Body, &reservation)
	if reservation.ID != 0 && err != nil {
		response.Failed(context, response.Ok().AddData("msg", "参数解析失败"))
		return
	}
	res := ReservationService.UpdateReservation(reservation)
	if res != 0 {
		response.Success(context, response.Ok().AddData("res", "修改成功"))
	} else {
		response.Failed(context, response.Error().AddData("res", "修改失败"))
	}
}

func (c *ReservationController) puts01(context *gin.Context) {
	var reservation model.Reservation
	err := tool.Decode(context.Request.Body, &reservation)
	if reservation.ID != 0 && err != nil {
		response.Failed(context, response.Ok().AddData("msg", "参数解析失败"))
		return
	}
	res := ReservationService.UpdateRefuseReservation(reservation)
	if res != 0 {
		response.Success(context, response.Ok().AddData("res", "修改成功"))
	} else {
		response.Failed(context, response.Error().AddData("res", "修改失败"))
	}
}

func (c *ReservationController) getPage(context *gin.Context) {
	var pageParam param.PageParam
	err := context.ShouldBind(&pageParam)
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := ReservationService.QueryReservations(pageParam)
	if res.Data != nil && res.Total != 0 {
		response.Success(context, response.Ok().AddData("res", res))
	} else {
		response.Failed(context, response.Error().AddData("res", res))
	}
}
func (c *ReservationController) getUserPage(context *gin.Context) {
	var pageParam param.PageParam
	err := context.ShouldBind(&pageParam)
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := ReservationService.QueryUserReservations(pageParam)
	if res.Data != nil && res.Total != 0 {
		response.Success(context, response.Ok().AddData("res", res))
	} else {
		response.Failed(context, response.Error().AddData("res", res))
	}
}
func (c *ReservationController) rsWork(context *gin.Context) {
	var pageParam param.PageParam
	err := context.ShouldBind(&pageParam)
	var userService = service.UserService{}
	pageParam.UserId = int(userService.QueryUserId(pageParam.UserName))
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	rs := ReservationService.CreateRsWork(pageParam)
	if rs != 0 {
		response.Success(context, response.Ok())
	} else {
		response.Failed(context, response.Error())
	}
}

func (c *ReservationController) rsSubsidy(context *gin.Context) {
	var pageParam param.PageParam
	err := context.ShouldBind(&pageParam)
	var userService = service.UserService{}
	pageParam.UserId = int(userService.QueryUserId(pageParam.UserName))
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	rs := ReservationService.CreateRsSubsidy(pageParam)
	if rs != 0 {
		response.Success(context, response.Ok())
	} else {
		response.Failed(context, response.Error())
	}
}

